package org.example.web;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.github.pagehelper.PageInfo;
import org.example.model.Attraction;
import org.example.model.Result;
import org.example.service.AttractionAdminService;
import org.example.util.AliYunImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/adminApi/attractions")
public class AttractionAdminController {
    @Autowired
    private AttractionAdminService attractionService;
    @RequestMapping(path = "/findList",method = RequestMethod.POST)
    public Result getAttractionList(@RequestBody Attraction attraction) {
        System.out.println(attraction.toString());
        PageInfo<Attraction> attractionList = attractionService.getAttractionList(attraction);
        return new Result(200, "success", attractionList);
    }
    @RequestMapping(path = "/delete",method = RequestMethod.GET)
    public Result deleteAttraction(int id) {
        attractionService.deleteAttraction(id);
        return new Result(200, "success", null);
    }
    @Autowired
    private AliYunImage aliYunImage;
    @RequestMapping(path = "/uploadImg",method = RequestMethod.POST)
    public Result uploadImg(@RequestParam("file") CommonsMultipartFile file) throws IOException {
        System.out.println(file.getInputStream());//获取图片流
        System.out.println(file.getOriginalFilename());
        String newFileName = new Date().getTime()+file.getOriginalFilename();
        //连接oss
        OSS ossClient = new OSSClientBuilder().build("https://"+aliYunImage.getEndpoint(),
                aliYunImage.getAccessKeyId(), aliYunImage.getAccessKeySecret());
        //长传文件
        ossClient.putObject(aliYunImage.getBucketName(), newFileName,
                file.getInputStream());
        //拼接文件在oss中访问地址
        String url = "https://"+aliYunImage.getBucketName()+"."+aliYunImage.getEndpoint()+"/"+newFileName;

        System.out.println("上传成功！！");
        return new Result(200, "上传成功", url);
    }
    @RequestMapping("/deleteImg")
    public Result deleteImg(@RequestParam("imgUrl") String imgUrl) {
        // 提取并清理文件名：去除首尾引号
        String fileName = imgUrl.substring(imgUrl.lastIndexOf("/") + 1)
                .replaceAll("^[\"']+|[\"']+$", "");
        System.out.println("删除文件：" + fileName);
        // 连接 OSS 并删除
        OSS ossClient = new OSSClientBuilder().build(
                "https://" + aliYunImage.getEndpoint(),
                aliYunImage.getAccessKeyId(),
                aliYunImage.getAccessKeySecret()
        );
        ossClient.deleteObject(aliYunImage.getBucketName(), fileName);
        ossClient.shutdown(); // 记得关闭客户端！原代码漏了，会导致连接泄漏

        return new Result(200, "删除成功", null);
    }
    @RequestMapping(path = "/save",method = RequestMethod.POST)
    public Result saveAttraction(@RequestBody Attraction attraction) {
        attractionService.saveAttraction(attraction);
        return new Result(200, "success", null);
    }






}
